#encoding:UTF-8
import re
import mylib
import pythonlibSB


def get_hot_link():
    
    try:
        results = []
        content = mylib.get_content('http://news.zing.vn/')
        hot_10_link = []
        
        hot_10_content = pythonlibSB.s_string(content,'id="featured_news"','div')
        hot_10_content = pythonlibSB.s_string(hot_10_content,'class="topnews','li')
        reg = re.compile('(<a)(.*?)(href=")(.*?)(")')
        if reg.search(hot_10_content):
            url = reg.search(hot_10_content).group(4) if reg.search(hot_10_content).group(4).find('http://')!=-1\
                    else ('http://news.zing.vn'+reg.search(hot_10_content).group(4))
            hot_10_link.append(url)
        results.append(hot_10_link)
        
        hot_9_link = []
        hot_9_content = pythonlibSB.s_string(content,'class="typicalevents"','ul')
        for i in re.finditer('(<a)(.*?)(href=")(.*?)(")',hot_9_content):
            hot9url = i.group(4) if i.group(4).find('http://')!=-1 else ('http://news.zing.vn'+i.group(4))
            hot_9_link.append(hot9url)
        results.append(hot_9_link)
        
        hot_8_link = []
        mycontent = pythonlibSB.s_string(content,'class="znews_leftcol"','div')
        pos1 = mycontent.find('class="showcase_listing"')
        while pos1 != -1:
            pos2 = mycontent.find('</ul>',pos1)
            if pos2 == -1:
                break
            t1 = mycontent[pos1:pos2]
            t = pythonlibSB.s_string(t1,'class="topnews"','li')
            reg2 = re.compile('(<a)(.*?)(href=")(.*?)(")')
            if reg2.search(t):
                url8 = reg2.search(t).group(4) if reg2.search(t).group(4).find('http://')!=-1\
                        else ('http://news.zing.vn'+reg2.search(t).group(4))
                if url8 in (hot_10_link,hot_9_link):
                    print 'Da trung'
                else:
                    hot_8_link.append(url8)
            
            pos1 = pos2 + 1
                    
        results.append(hot_8_link)
        results.append([])
        
        return results
    except:
        return []
    
    
    
if __name__ == "__main__":
    results = get_hot_link()
    print results
    print results[2]
    print len(results[2])